package fr.alma.ihm;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import fr.alma.coeur.GestionPlugins;

/**
 * Classe de gestion de lien entre plugins.
 * @author AUBERT Sebastien
 * @author BOUVET Frederic
 * @author BRAUD Jeremy
 * @author KROMMENHOEK Cedric
 *
 */
public class ConfigurationPlugin extends JDialog {

	/**
	 * Generated serial ID.
	 */
	private static final long serialVersionUID = 5001531807634931553L;

	/**
	 * Le nom du plugin d'entree.
	 */
	private String pluginEntree;
	/**
	 * La classe de gestion des plugins.
	 */
	private GestionPlugins gestionPlugins;
	/**
	 * Liste des plugins transformateurs.
	 */
	private ArrayList<String> pluginsTransformateurs;
	/**
	 * Liste des plugins de sortie.
	 */
	private ArrayList<String> pluginsSortie;
	
	/**
	 * Constructeur.
	 * @param parent la fenetre appelante
	 * @param title le titre de la fenetre
	 * @param modal si la fenetre est modale
	 * @param plugin le plugin d'entree
	 * @param gestion la classe de gestion des plugins
	 */
	public ConfigurationPlugin(JFrame parent, String title, boolean modal, String plugin, GestionPlugins gestion) {
		super(parent, title, modal);
		this.pluginEntree = plugin;
		this.gestionPlugins = gestion;
		this.pluginsTransformateurs = new ArrayList<String>();
		this.pluginsSortie = new ArrayList<String>();
		this.setSize(550, 250);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.initComponent();
	}

	/**
	 * Initialise la fenetre.
	 */
	private void initComponent() {
		ArrayList<String> listePluginTransformateurs;
		listePluginTransformateurs = this.gestionPlugins.listePluginsTransformateur(this.pluginEntree);
		ArrayList<String> listePluginSorties = this.gestionPlugins.listePluginsSortie(this.pluginEntree);
		JPanel panPluginInter = new JPanel();
		panPluginInter.setBorder(BorderFactory.createTitledBorder("Liste des plugins transformateurs:"));
		for (final String plugin : listePluginTransformateurs) {
			JCheckBox selection = new JCheckBox(plugin);
			selection.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (((AbstractButton) arg0.getSource()).isSelected()) {
						pluginsTransformateurs.add(plugin);
					} else {
						pluginsTransformateurs.remove(plugin);
					}
				}
			});
			panPluginInter.add(selection);
		}
		
		JPanel panPluginSortie = new JPanel();
		panPluginSortie.setBorder(BorderFactory.createTitledBorder("Liste des plugins de sortie:"));
		for (final String plugin : listePluginSorties) {
			JCheckBox selection = new JCheckBox(plugin);
			selection.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (((AbstractButton) arg0.getSource()).isSelected()) {
						pluginsSortie.add(plugin);
					} else {
						pluginsSortie.remove(plugin);
					}
				}
			});
			panPluginSortie.add(selection);
		}
		
		JPanel control = new JPanel();
		JButton okBouton = new JButton("OK");
		okBouton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> listePlugins = new ArrayList<String>();
				listePlugins.addAll(pluginsTransformateurs);
				listePlugins.addAll(pluginsSortie);
				gestionPlugins.ajoutLiens(pluginEntree, listePlugins);
				setVisible(false);
			}
		});
		JButton cancelBouton = new JButton("Annuler");
		cancelBouton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}			
		});
		control.add(okBouton);
		control.add(cancelBouton);

		this.getContentPane().add(panPluginInter, BorderLayout.NORTH);
		this.getContentPane().add(panPluginSortie, BorderLayout.CENTER);
		this.getContentPane().add(control, BorderLayout.SOUTH);
	}

}
